import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'

Vue.use(VueRouter)
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  // {
  //   path: '/free',
  //   name: 'free',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  // },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    redirect: '/user/cart',
    children: [
      {
        path: 'cart',
        component: () => import('../components/user/Cart.vue')
      }
    ]
    // beforeEnter: (to, from, next) => {
    //   /* must call `next` */
    //   let token = localStorage.getItem('x-auth-token');
    //   if (!token) {
    //     store.dispatch('showToast/asyncChangeIsShowToast', {
    //       isShow: true,
    //       msg: '请先登录!',
    //       type: 'warning'
    //     })
    //     store.commit('loginModal/changeModal', true);
    //     return;
    //   }
    //   next();
    // }
  },
  // {
  //   path: '/order',
  //   name: 'order',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  // },
  {
    path: '/detail',
    name: 'detail',
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  },
  {
    path: '*',
    component: () => import('../components/public/error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem('x-auth-token');
//   if (to.path == '/user' && !token) {
//     store.dispatch('showToast/asyncChangeIsShowToast', {
//       isShow: true,
//       msg: '请先登录!',
//       type: 'warning'
//     })
//     store.commit('loginModal/changeModal', true);
//     return;
//   }
//   next();
// })
export default router
